package com.google.code.mochaccino.framework.crypto;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.crypto.cipher.CipherHandlerBlockDoFinal;
import com.google.code.mochaccino.framework.crypto.cipher.CipherHandlerBlockUpdate;
import com.google.code.mochaccino.framework.crypto.cipher.CipherHandlerDoFinal;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemeAsymmetric;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemePasswordBased;
import com.google.code.mochaccino.framework.crypto.scheme.impl.EncryptionSchemeSymmetric;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import org.junit.Test;

public class EncryptionTest_CipherHandler extends EncryptionTest {
	@Test
	public void testCipherHandlerDES() throws EncryptionException {
		byte[] key = EncryptionSchemeSymmetric.generateKey( "DES", EncryptionProvider.getBouncyCastleName() );
		EncryptionSchemeSymmetric enc1 = new EncryptionSchemeSymmetric( key, "DES", EncryptionProvider.getBouncyCastleName(), new CipherHandlerDoFinal() );
		EncryptionSchemeSymmetric enc2 = new EncryptionSchemeSymmetric( key, "DES", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		EncryptionSchemeSymmetric enc3 = new EncryptionSchemeSymmetric( key, "DES", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		checkCipherHandlers( enc1, enc2, enc3 );
	}

	@Test
	public void testCipherHandlerDESede() throws EncryptionException {
		byte[] key = EncryptionSchemeSymmetric.generateKey( "DESede", EncryptionProvider.getBouncyCastleName() );
		EncryptionSchemeSymmetric enc1 = new EncryptionSchemeSymmetric( key, "DESede", EncryptionProvider.getBouncyCastleName(), new CipherHandlerDoFinal() );
		EncryptionSchemeSymmetric enc2 = new EncryptionSchemeSymmetric( key, "DESede", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		EncryptionSchemeSymmetric enc3 = new EncryptionSchemeSymmetric( key, "DESede", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		checkCipherHandlers( enc1, enc2, enc3 );
	}

	@Test
	public void testCipherHandlerPBE_2Fish() throws EncryptionException {
		EncryptionSchemePasswordBased enc1 = new EncryptionSchemePasswordBased( "test", 256, "test", 16, "PBEWithSHAAndTwofish-CBC", EncryptionProvider.getBouncyCastleName(), new CipherHandlerDoFinal() );
		EncryptionSchemePasswordBased enc2 = new EncryptionSchemePasswordBased( "test", 256, "test", 16, "PBEWithSHAAndTwofish-CBC", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		EncryptionSchemePasswordBased enc3 = new EncryptionSchemePasswordBased( "test", 256, "test", 16, "PBEWithSHAAndTwofish-CBC", EncryptionProvider.getBouncyCastleName(), new CipherHandlerBlockUpdate() );
		checkCipherHandlers( enc1, enc2, enc3 );
	}

	@Test
	public void testCipherHandlerRSA() throws NoSuchAlgorithmException, EncryptionException, NoSuchProviderException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "RSA", EncryptionProvider.getBouncyCastleName() );
		keyPairGenerator.initialize( 2048 );
		KeyPair kp = keyPairGenerator.generateKeyPair();
		EncryptionSchemeAsymmetric enc1 = new EncryptionSchemeAsymmetric( "RSA", EncryptionProvider.getBouncyCastleName(), kp.getPublic(), kp.getPrivate(), new CipherHandlerDoFinal() );
		EncryptionSchemeAsymmetric enc2 = new EncryptionSchemeAsymmetric( "RSA", EncryptionProvider.getBouncyCastleName(), kp.getPublic(), kp.getPrivate(), new CipherHandlerBlockDoFinal() );
		EncryptionSchemeAsymmetric enc3 = new EncryptionSchemeAsymmetric( "RSA", EncryptionProvider.getBouncyCastleName(), kp.getPublic(), kp.getPrivate(), new CipherHandlerBlockUpdate() );
		checkCipherHandlers( enc1, enc2, enc3 );
	}
}
